<?php
namespace app\admin\controller;

use app\admin\model\AlModel;
class Al extends BaseAdmin{
    private $model = "";
    public function _initialize(){
        parent::_initialize();
        $this->model = new AlModel;
    }
    public function type(){
        $list = Db('al_type')->order('ty_id desc')->paginate(10);
        $this->assign("list",$list);
        return view('type');
    }
    public function save_type(){
        if($this->request->isAjax()){
            $id=$_POST['id'];
            if($id){
                $data['ty_name']=\input('name');
                $data['ty_ename']=\input('ename');
                $res=$this->model->updateNewstype("ty_id=$id",$data);
                if($res){
                    $this->success("修改成功！",url('type'));
                }else{
                    $this->error("修改失败！",url('type'));
                }
            }else{
                $data['ty_name']=\input('name');
                $data['ty_ename']=\input('ename');
                $re=$this->model->addNewstype($data);
                if($re){
                    $this->success("添加成功！",url('type'));
                }else{
                    $this->error("添加失败！",url('type'));
                }
            }
    
        }else{
            $this->success("非法提交",url('type'));
        }
    }
    function delete_type(){
        $id=input('id');
        $del=$this->model->deleteType($id);
        $this->redirect('type');
    }
    public function modify(){
        $id=input('id');
        $re=db('al_type')->where("ty_id=$id")->find();
        echo json_encode($re);
    }
    public function lister(){
        $list=db('al')->alias('a')->join("al_type b","a.fid = b.ty_id")->order(['sort'=>'asc','id'=>'desc'])->paginate(10);
        $this->assign("list",$list);
        $page=$list->render();
        $this->assign("page",$page);
        return view('lister');
    }
    public function add(){
        $res=db('al_type')->select();
        $this->assign("res",$res);
        
        return view('add');
    }
    public function save(){
        if(!is_string(input('image'))){
            $data['image']=uploads('image');
        }
        if(input('status')){
            $data['status']=1;
        }
        if(input('groom')){
            $data['groom']=1;
        }
        $data['time']=time();
        $data['fid']=input('fid');
        $data['title']=input('title');
        $data['source']=input('source');
        $data['writer']=input('writer');
        $data['sort']=input('sort');
        $data['marray']=input('marray');
        $data['content']=input('content');
        $re=$this->model->addNews($data);
        if($re){
            $this->success("添加成功！",url('add'));
        }else{
            $this->error("修改失败！");
        }
    }
    public function change(){
        $id=$_POST['id'];
        $res=$this->model->updateStatus($id);
        if($res){
            echo '1';
        }else{
            echo '0';
        }
    }
    public function changes(){
        $id=$_POST['id'];
        $res=$this->model->updateGroom($id);
        if($res){
            echo '1';
        }else{
            echo '0';
        }
    }
    public function modifys(){
        $res=db('al_type')->select();
        $this->assign("res",$res);
        $id=input('id');
        $re=db('al')->where("id=$id")->find();
        $this->assign("re",$re);
        return view('modifys');
    }
    public function usave(){
        $id=input('id');
        $re=db('al')->where("id=$id")->find();
        if(!is_string(input('image'))){
            $data['image']=uploads('image');
            
        }
        if(input('status')){
            $data['status']=1;
        }
        if(input('groom')){
            $data['groom']=1;
        }
        $data['fid']=input('fid');
        $data['title']=input('title');
        $data['source']=input('source');
        $data['writer']=input('writer');
        $data['sort']=input('sort');
        $data['marray']=input('marray');
        $data['content']=input('content');
        $res=$this->model->updateNews("id=$id", $data);
        if($res){
            $this->success("修改成功！",url('lister'));
        }else{
            $this->error("修改失败！");
        }
    }
    public function delete(){
        $id=input('id');
        $del=$this->model->deleteNews($id);
        $this->redirect('lister');
    }
    public function sort(){
        $data=input('post.');
        $al=db('al');
        foreach ($data as $id => $sort){
            $al->where(array('id' => $id ))->setField('sort' , $sort);
        }
        $this->redirect('lister');
    }
    public function delete_all(){
        $id=input('id');
        $arr=explode(",", $id);
        $del=$this->model->deleteAll($arr);
        $this->redirect('lister');
    }
    
    
    
    
    
    
    
    
    
    
}